import pygame
from mods import CrystalLib
import mods.cfg
import mods.printlib
from random import randint as ri

def draw_block(x, y, dtype, screen):
    # x=1~20,  y=1~15
    #global screen
    if dtype == CrystalLib.Void:
        pygame.draw.rect(screen, (255,255,255), (CrystalLib.cint(x*60), CrystalLib.cint(y*60), 60, 60))
    elif dtype == CrystalLib.Soft:
        pygame.draw.rect(screen, (180, 120, 30), (CrystalLib.cint(x*60), CrystalLib.cint(y*60), 60, 60))
    elif dtype == CrystalLib.Hard:
        pygame.draw.rect(screen, (10, 10, 20), (CrystalLib.cint(x*60), CrystalLib.cint(y*60), 60, 60))
    elif dtype == CrystalLib.Bed:
        pygame.draw.rect(screen, (0, 255, 0), (CrystalLib.cint(x*60), CrystalLib.cint(y*60), 60, 60))
    elif dtype == CrystalLib.BB:
        pygame.draw.rect(screen, (255, 90, 60), (CrystalLib.cint(x*60), CrystalLib.cint(y*60), 60, 60))
    else:
        CrystalLib.print_warning(f'无法加载的区块: ({x}, {y}, {dtype}).')

def draw_entity(who, screen):
    if who.ID == 0:
        pygame.draw.ellipse(screen, (0, 0x80, 0xff),
                            (CrystalLib.cint(who.x*60 + who.xp), CrystalLib.cint(who.y*60 + who.yp + 10), 60, 40))
    elif who.ID == 1:
        pygame.draw.ellipse(screen, mods.cfg.color_e1,
                            (CrystalLib.cint(who.x*60 + who.xp), CrystalLib.cint(who.y*60 + who.yp + 10), 60, 40))
    elif who.ID == 2:
        pygame.draw.ellipse(screen, mods.cfg.color_e2,
                            (CrystalLib.cint(who.x*60 + who.xp), CrystalLib.cint(who.y*60 + who.yp + 10), 60, 40))
    elif who.ID == 3:
        pygame.draw.ellipse(screen, mods.cfg.color_e3,
                            (CrystalLib.cint(who.x*60 + who.xp), CrystalLib.cint(who.y*60 + who.yp + 10), 60, 40))

def draw_item(who, screen):
    if who.ID == 4:
        #Bomb
        pygame.draw.rect(screen, who.color, (CrystalLib.cint(who.x*60), CrystalLib.cint(who.y*60), 60, 60))
    elif who.ID == 5:
        pygame.draw.rect(screen, who.color, (CrystalLib.cint(who.x*60), CrystalLib.cint(who.y*60), 60, 60))
        if who.towards == 1:
            Ellipsis
        #    pygame.draw.rect(screen, (255,255,255), (CrystalLib.cint(who.x*60+20), CrystalLib.cint(who.y*60), 20, 60))
        elif who.towards == 2:
            Ellipsis
        #    pygame.draw.rect(screen, (255,255,255), (CrystalLib.cint(who.x*60), CrystalLib.cint(who.y*60+20), 60, 20))
        else:
            pygame.draw.rect(screen, (255,255,255), (CrystalLib.cint(who.x*60+20), CrystalLib.cint(who.y*60), 20, 60))
            pygame.draw.rect(screen, (255,255,255), (CrystalLib.cint(who.x*60), CrystalLib.cint(who.y*60+20), 60, 20))
    elif who.ID == 6:
        pygame.draw.rect(screen, (255, 0, 0), (CrystalLib.cint(who.x*60 + 8), CrystalLib.cint(who.y*60 + 8), 44, 44))
        mods.printlib.CenterPrint1('TNT', (255,255,255), (CrystalLib.cint(who.x*60 + 30), CrystalLib.cint(who.y*60 + 30)), screen)

Unknown = pygame.Surface((50, 50))
pygame.draw.rect(Unknown, (255, 0, 255), (0, 25, 25, 25))
pygame.draw.rect(Unknown, (255, 0, 255), (25, 0, 25, 25))

color_add = (ri(180, 255), ri(0, 255), ri(0, 150))
color_reduce = (ri(0, 150),ri(0, 255), ri(180, 255))
add_pic = pygame.Surface((40, 40))
pygame.draw.rect(add_pic, color_add, (0, 0, 40, 40))
pygame.draw.rect(add_pic, (255,255,255), (5, 5, 30, 30))
pygame.draw.rect(add_pic, color_add, (18, 7, 4, 26))
pygame.draw.rect(add_pic, color_add, (7, 18, 26, 4))

reduce_pic = pygame.Surface((40, 40))
pygame.draw.rect(reduce_pic, color_reduce, (0, 0, 40, 40))
pygame.draw.rect(reduce_pic, (255,255,255), (5, 5, 30, 30))
pygame.draw.rect(reduce_pic, color_reduce, (7, 18, 26, 4))

nextpage = pygame.Surface((120, 40))
nextpage.fill(CrystalLib.RandomColor())
pygame.draw.rect(nextpage, (ri(200, 255), ri(200, 255), ri(200, 255)), (4, 4, 112, 32))
mods.printlib.CenterPrint('下一页', CrystalLib.RandomColor(), (60, 20), nextpage)

ShaderSetting = pygame.Surface((120, 40))
ShaderSetting.fill(CrystalLib.RandomColor())
pygame.draw.rect(ShaderSetting, (ri(200, 255), ri(200, 255), ri(200, 255)), (4, 4, 112, 32))
mods.printlib.CenterPrint('光影设置', CrystalLib.RandomColor(), (60, 20), ShaderSetting)

def draw_pic(which, pos : tuple, screen):
    if which == 'add':
        screen.blit(add_pic, pos)
    elif which == 'reduce':
        screen.blit(reduce_pic, pos)
    elif which == 'nextpage':
        screen.blit(nextpage, pos)
    elif which == 'ShaderSetting':
        screen.blit(ShaderSetting, pos)
    else:
        screen.blit(Unknown, pos)

_SdName = 'Blockworld v1.0'
_SdName = 'Blockworld v2.0' # add Iris mod, 区分不同敌人颜色
_SdName = 'Blockworld v3.0' # add function [draw_pic]
def Shader_name():
    CrystalLib.printm(f'成功加载shader: {_SdName}')
    return _SdName
def on_inits():
    pass
def inits(_sc_reen = None):
    return 'None', on_inits
def draw_particle_enderpearl(x: int, y: int):
    ...